package cn.tedfu.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

@RestController
@RequestMapping("user")
public class CarController3 {
    @RequestMapping("save")
    public void save(Integer id,String name,Integer age) throws Exception {
        /* 把解析出来的参数，利用jdbc技术入库 */
        //1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.获取链接
        String url ="jdbc:mysql:///cgb2104?characterEncoding=utf8&useSSL=false";
        //"jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false"
        Connection conn = DriverManager.getConnection(url,"root","admin123");
        //获取传输器
        String sql= "insert into user values(?,?,?)";//所有字段设置值
        PreparedStatement ps = conn.prepareStatement(sql);
        //给SQL设置参数
        ps.setInt(1,id);//给第一个？设置值
        ps.setString(2,name);//给第二个？设置值
        ps.setInt(3,age);//给第三个？设置值
        //执行SQL
        int rows = ps.executeUpdate();
        //释放资源 -- OOM(OutOfMemory)
        ps.close();
        conn.close();
    }
    @RequestMapping("get2/{id}/{name}/{age}")
    public void get2(@PathVariable Integer id,
                     @PathVariable String name,
                     @PathVariable Integer age
    ){
        System.out.println(id+name+age);
    }
}
